package com.example.blog.mapper;

import com.example.blog.entity.Article;
import com.example.blog.entity.Comment;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ArticleMapper {
    @Select("SELECT * FROM article WHERE id = #{id}")
    Article selectArticleById(int id);

    // 新增方法，用于获取文章并加锁，以便更新评论数时防止并发冲突
    @Select("SELECT * FROM article WHERE id = #{id} FOR UPDATE")
    Article selectArticleByIdForUpdate(int id);

    @Update("UPDATE article SET comments = #{comments} WHERE id = #{id}")
    int updateComments(Article article);

    @Delete("DELETE FROM article WHERE id = #{id}")
    int deleteArticleById(int id);
    @Select("SELECT * FROM article")
    List<Article> selectAllArticles();

    @Update("UPDATE article SET title = #{title}, content = #{content} WHERE id = #{id}")
    int updateArticle(Article article);



    @Select("SELECT * FROM article WHERE title LIKE CONCAT('%', #{keyword}, '%') OR content LIKE CONCAT('%', #{keyword}, '%')")
    List<Article> searchArticles(String keyword);
}